package com.berry.MedCheck;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.*;
import com.berry.MedCheck.db.UserDB;
import com.berry.MedCheck.widget.LabelEditText;
import com.berry.MedCheck.widget.LabelText;

/**
 * Created with IntelliJ IDEA.
 * User: Airay.Rong
 * Date: 13-12-4
 * Time: 上午10:06
 * To change this template use File | Settings | File Templates.
 */
public class AddNewUserActivity extends BaseActivity implements View.OnClickListener {
	private static final String TAG = "AddNewUserActivity";

	private TextView mBtnDetail;
	private TextView mBtnSave;
	private LinearLayout mDetailPart;

	private EditText mName;
	private CheckBox mCheckMale;
	private CheckBox mCheckFemale;
	private EditText mAge;
	private EditText mHeight;
	private EditText mWeight;
	private Uri mUri;
	private Cursor mCursor;

	private static final String[] PROJECTION = new String[]{
			UserDB.DBColumns._ID, // 0
			UserDB.DBColumns.USERNAME, // 2
			UserDB.DBColumns.SEX, // 3
			UserDB.DBColumns.AGE, // 4
			UserDB.DBColumns.HEIGHT, // 5
			UserDB.DBColumns.WEIGHT, // 6
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_new_user_activity);
		mBtnDetail = (TextView) findViewById(R.id.details);
		mBtnDetail.setOnClickListener(this);

		mBtnSave = (TextView) findViewById(R.id.save);
		mBtnSave.setOnClickListener(this);
		mDetailPart = (LinearLayout) findViewById(R.id.detail_part);
		mDetailPart.setVisibility(View.GONE);

		mName = (EditText) findViewById(R.id.et_name);
		mCheckMale = (CheckBox) findViewById(R.id.check_male);
		mCheckMale.setOnClickListener(this);
		mCheckFemale = (CheckBox) findViewById(R.id.check_female);
		mCheckFemale.setOnClickListener(this);
		mAge = (EditText) findViewById(R.id.et_age);
		mAge.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		mHeight = (EditText) findViewById(R.id.et_height);
		mHeight.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		mWeight = (EditText) findViewById(R.id.et_weight);
		mWeight.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

	}

	@Override
	public void onClick(View v) {
		if (v == mBtnDetail) {
			if (mDetailPart.getVisibility() == View.GONE) {
				mDetailPart.setVisibility(View.VISIBLE);
			} else {
				mDetailPart.setVisibility(View.GONE);
			}

		}
		if (v == mBtnSave) {
			int result = checkInput();
			if (result == 0) {
				saveNewUser();

			} else if (result == 1) {
				showAlertDialog_NameEmpty();
			} else if (result == 2) {
				showAlertDialog_InfoEmpty();
			}
		}
		if (v == mCheckFemale) {
			mCheckMale.setChecked(false);
		}
		if (v == mCheckMale) {
			mCheckFemale.setChecked(false);
		}
	}

	private int checkInput() {
		if (mName.getText().toString().trim().isEmpty()) {
			return 1;
		}
		if (mAge.getText().toString().trim().isEmpty()
				|| mHeight.getText().toString().trim().isEmpty() ||
				mWeight.getText().toString().trim().isEmpty()) {
			return 2;
		}
		return 0;  //To change body of created methods use File | Settings | File Templates.
	}
	public void showAlertDialog_InfoEmpty() {
		final AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setCanceledOnTouchOutside(false);
		alert.show();

		Window window = alert.getWindow();
		window.setContentView(R.layout.my_alert_dialog);
		TextView tv = (TextView) window.findViewById(R.id.message);
		tv.setText(this.getResources().getText(R.string.user_info_empty));
		Button btnCancel = (Button) window.findViewById(R.id.btn_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				alert.cancel();

			}
		});
		Button btnOK = (Button) window.findViewById(R.id.btn_ok);
		btnOK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				saveNewUser();
			}
		});

	}
	public void showAlertDialog_NameEmpty() {
		final AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setCanceledOnTouchOutside(false);
		alert.show();
		Window window = alert.getWindow();
		window.setContentView(R.layout.my_alert_dialog);
		TextView tv = (TextView) window.findViewById(R.id.message);
		tv.setText(this.getResources().getText(R.string.user_name_empty));
		Button btnCancel = (Button) window.findViewById(R.id.btn_cancel);
		btnCancel.setVisibility(View.GONE);
		Button btnOK = (Button) window.findViewById(R.id.btn_ok);
		btnOK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				alert.cancel();
			}
		});
	}
	public void showAlertDialog_SaveDone() {
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setCanceledOnTouchOutside(false);
		alert.show();
		Window window = alert.getWindow();
		window.setContentView(R.layout.my_alert_dialog);
		TextView tv = (TextView) window.findViewById(R.id.message);
		tv.setText(this.getResources().getText(R.string.save_done));
		Button btnCancel = (Button) window.findViewById(R.id.btn_cancel);
		btnCancel.setVisibility(View.GONE);
		Button btnOK = (Button) window.findViewById(R.id.btn_ok);
		btnOK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	private void saveNewUser() {
		String userName = mName.getText().toString();
		int userSex= 0;
		int userAge= 0;
		int userHeight= 0;
		int userWeight= 0;
		if (mCheckFemale.isChecked() == true) {
			userSex = 1;
		}
		if (mAge.getText().length() != 0) {
			userAge = Integer.decode(mAge.getText().toString());
		}
		if (mHeight.getText().length() != 0) {
			userHeight = Integer.decode(mHeight.getText().toString());
		}
		if (mWeight.getText().length() != 0) {
			userWeight = Integer.decode(mWeight.getText().toString());
		}


		mUri = getContentResolver().insert(UserDB.DBColumns.CONTENT_URI, null);
		mCursor = managedQuery(mUri, PROJECTION, null, null, null);
		if (mCursor != null) {
			ContentValues values = new ContentValues();
			values.put(UserDB.DBColumns.USERNAME, userName);
			values.put(UserDB.DBColumns.SEX, userSex);
			values.put(UserDB.DBColumns.AGE, userAge);
			values.put(UserDB.DBColumns.HEIGHT, userHeight);
			values.put(UserDB.DBColumns.WEIGHT, userWeight);
			try {
				getContentResolver().update(mUri, values, null, null);
				showAlertDialog_SaveDone();

			} catch (NullPointerException e) {
				Log.e(TAG, e.getMessage());
			}

		}



	}
}